package com.haojin.spring.config;

import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Set;

/**
 * @author .29.
 * @create 2023-03-01 11:39
 */
//使用java原生的jakarta.validation.Validator校验
@Service
public class MyService1 {
    @Autowired   //自动装配Validator对象
    private Validator validator;

    //校验方法
    public boolean validator(User user){
        //校验后的结果存放进Set集合
        Set<ConstraintViolation<User>> set = validator.validate(user);
        //若没有校验到错误，集合为空，返回true。
        return set.isEmpty();
    }
}
